package com.wuzihao.disk.file.control;

import com.wuzihao.disk.file.entity.SearchFileEntity;
import com.wuzihao.disk.file.service.FileIoService;
import com.wuzihao.disk.mybatis.entity.PtFileEntity;
import com.wuzihao.disk.mybatis.entity.PtRoleEntity;
import com.wuzihao.disk.mybatis.entity.PtUserEntity;
import com.wuzihao.disk.mybatis.flexigrid.PagingEntity;
import com.wuzihao.disk.mybatis.flexigrid.TblResponseEntity;
import com.wuzihao.disk.share.service.impl.ShareServiceImpl;
import com.wuzihao.disk.spring.control.BaseController;

import com.wuzihao.disk.file.service.FileService;
import com.wuzihao.disk.spring.control.BaseController;
import com.wuzihao.disk.user.entity.SearchUserEntity;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
    @Resource
    private FileService fileService;

    @Resource
    private FileIoService fileIoService;

    @RequestMapping("/fileAll")
    public String fileAll() {
        return "file/filePage";
    }

    //查询文件列表
    @RequestMapping("/queryFileList")
    @ResponseBody
    public List<PtFileEntity> queryFileList(){
        List<PtFileEntity> list = fileService.queryFileList();
        return list;
    }

    //分页查询
    @RequestMapping("/queryFileInfoListByPage")
    @ResponseBody
    public TblResponseEntity<PtFileEntity> queryFileInfoListByPage(SearchFileEntity entity, PagingEntity paging) {
        TblResponseEntity<PtFileEntity> responseEntity = fileService.queryFileInfoListByPage(entity, paging);
        return responseEntity;
    }

    @RequestMapping("/queryFileInfoById")
    @ResponseBody
    public PtFileEntity queryFileInfoById(@RequestParam("fileUuid") String fileUuid) {
        PtFileEntity ptFileEntity = fileService.queryFileInfoById(fileUuid);
        return ptFileEntity;
    }

    @RequestMapping("/syncFileData")
    @ResponseBody
    public String syncFileData(){
        fileIoService.syncFileData();
        return successJson("file.sync.data.success");
    }

    /**
     * 文件上传接口
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("files") MultipartFile[] files, @RequestParam("targetDir") String targetDir) {
        try {
            // 调用服务层处理文件上传
            boolean success = fileService.uploadFiles(files, targetDir);
            if (success) {
                return successJson("file.upload.success");
            } else {
                return failJson("file.upload.fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return failJson("file.upload.error", e.getMessage());
        }
    }

    @RequestMapping("/queryDirectoryTree")
    @ResponseBody
    public List<Map<String, Object>> queryDirectoryTree() {
        return fileService.queryDirectoryTree();
    }

    @RequestMapping("/copyFile")
    @ResponseBody
    public Map<String, Object> copyFile(@RequestParam("fileUuid") String fileUuid, @RequestParam("targetDir") String targetDir) {
        Map<String, Object> result = new HashMap<>();
        try {
            fileService.copyFile(fileUuid, targetDir);
            result.put("success", true);
            result.put("msg", messageSource.getMessage("file.copy.success", null, LocaleContextHolder.getLocale()));
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", e.getMessage());
            logger.error("copyFile error", e);
        }
        return result;
    }

    @RequestMapping("/queryFileNameByUuid")
    @ResponseBody
    public Map<String, Object> queryFileNameByUuid(@RequestParam("fileUuid") String fileUuid) {
        Map<String, Object> result = new HashMap<>();
        try {
            String fileName = fileService.queryFileNameByUuid(fileUuid);
            result.put("fileName", fileName);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", messageSource.getMessage("file.query.fail", null, LocaleContextHolder.getLocale()));
            logger.error("queryFileNameByUuid error", e);
        }
        return result;
    }

    @RequestMapping("/mkdirFileInfo")
    @ResponseBody
    public String mkdirFileInfo(@RequestParam("parentId") String parentId,@RequestParam("folderName") String folderName) {
        fileIoService.mkdirFileInfo(parentId,folderName);
        return successJson("file.mkdir.success");
    }

    @RequestMapping("/getShareUrl")
    @ResponseBody
    public String getShareUrl(@RequestParam("fileUuid") String fileUuid){
        String url = fileIoService.getShareUrl(fileUuid);
        return successJson("file.share.success",url);
    }

    @RequestMapping("/queryShareUrl")
    @ResponseBody
    public String queryShareUrl(@RequestParam("fileUuid") String fileUuid,@RequestParam("fileValidity") Integer fileValidity){
        String url = fileIoService.queryShareUrl(fileUuid,fileValidity);
        return successJson("file.share.success",url);
    }

    @RequestMapping("/modifyFileInfoById")
    @ResponseBody
    public String modifyFileInfoById(PtFileEntity entity) {
        fileService.modifyFileInfoById(entity);
        return successJson("modify.file.success");
    }

}























